import { storeToRefs } from "pinia"
import axiosAPI from ".."
import appStore from "@/stores"

var errorMsg = ""
const { token } = storeToRefs(appStore.userStore)
const { switchTipStatus } = appStore.homeviewStore

/**
 * 获取所有博客列表
 * @param {string} mode - 模式 ["homepageBlogs", "archiveBlogs", "topBlogs"]
 * @param {function} callback - 成功回调，参数为博客数据
 * @param {function} errorback - 失败回调
 * @returns {void}
 */
export async function GetBlogsAll(mode, callback, errorback) {
  axiosAPI("get", "/api/blogs/all/" + mode)
    .then((rData) => {
      callback(rData)
    })
    .catch((error) => {
      switchTipStatus({ level: "warning", msg: "获取所有博客失败！请重试！" })
    })
}

/**
 * 获取指定博客详情
 * @param {string|number} blog_id - 博客ID
 * @param {function} callback - 成功回调，参数为博客详情
 * @param {function} errorback - 失败回调
 * @returns {void}
 */
export async function GetBlogDetail(blog_id, callback, errorback) {
  axiosAPI("get", "/api/blogs/" + blog_id + "/")
    .then((rData) => {
      callback(rData)
    })
    .catch((error) => {
      switchTipStatus({ level: "warning", msg: "获取博客失败！请重试！" })
      //   errorback(error)
    })
}

/**
 * 新增博客
 * @param {Object} data - 博客数据
 * @param {function} callback - 成功回调，参数为响应数据
 * @param {function} errorback - 失败回调
 * @returns {void}
 */
export async function AddBlog(data, callback, errorback) {
  axiosAPI(
    "post",
    "/api/blogs/add/",
    {
      Authorization: `Bearer ${token.value}`,
      "Content-Type": "application/json",
    },
    {},
    data
  )
    .then((rData) => {
      callback(rData)
      switchTipStatus({ level: "info", msg: "创建博客成功" })
    })
    .catch((error) => {
      if (error.status === 413) {
        errorMsg = "请缩减博客，最大不超过10M"
      } else if (error.status === 401) {
        errorMsg = "无权限，请先登录"  
      } else {
        errorMsg = "创建博客失败！请重试！"
      }
      switchTipStatus({ level: "warning", msg: errorMsg })
      errorback()
    })
}

/**
 * 编辑博客
 * @param {string|number} blog_id - 博客ID
 * @param {Object} data - 博客数据
 * @param {function} callback - 成功回调
 * @param {function} errorback - 失败回调
 * @returns {void}
 */
export async function EditBlog(blog_id, data, callback, errorback) {
  axiosAPI(
    "put",
    "/api/blogs/" + blog_id + "/",
    {
      Authorization: `Bearer ${token.value}`,
    },
    {},
    data
  )
    .then(() => {
      switchTipStatus({ level: "info", msg: "编辑博客成功" })
      callback()
    })
    .catch((error) => {
      if (error.status === 401) {
        errorMsg = "无权限，请先登录"
      } else {
        errorMsg = "编辑博客失败！请重试！"
      }
      switchTipStatus({ level: "warning", msg: errorMsg })
      errorback()
    })
}

/**
 * 删除博客
 * @param {string|number} blog_id - 博客ID
 * @param {function} callback - 成功回调
 * @param {function} errorback - 失败回调
 * @returns {void}
 */
export async function DeleteBlog(blog_id, callback, errorback) {
  axiosAPI("delete", "/api/blogs/" + blog_id + "/", {
    Authorization: `Bearer ${token.value}`,
    "Content-Type": "application/json",
  })
    .then(() => {
      callback()
      switchTipStatus({ level: "info", msg: "删除博客成功" })
    })
    .catch((error) => {
      if (error.status === 401) {
        errorMsg = "无权限，请先登录"
      } else {
        errorMsg = "删除博客失败！请重试！"
      }
      switchTipStatus({ level: "warning", msg: errorMsg })
      //   errorback(error)
    })
}

export async function UpdateBlogView(id, callback, errorback) {
  axiosAPI("get", "/api/blogviews/" + id + "/")
    .then(() => {
      callback()
    })
    .catch(() => {
      switchTipStatus({ level: "warning", msg: "网络错误, 请重试!" })
      //   errorback(error)
    })
}
